//
//  HMRealName.m
//  SNS_Login-register
//
//  Created by hy on 2021/10/29.
//  Copyright © 2021 lzxuan. All rights reserved.
//

#import "HWRealNameView.h"
#import "HWGameMgr.h"
#import "HWConstant.h"

@interface HWRealNameView()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UITextView *tips;

@property (weak, nonatomic) IBOutlet UITextField *userAccountTextField;
@property (weak, nonatomic) IBOutlet UITextField *NameTextField;
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@end

@implementation HWRealNameView


+ (instancetype)HWRealNameView{
    NSString  *clsName = NSStringFromClass([self class]);
    UINib * nib = [UINib nibWithNibName:clsName bundle:nil];
    HWRealNameView * obj = [nib instantiateWithOwner:nil options:nil].firstObject;
    [obj initGameView];
    return obj;
    
}


-(void)updateData:(NSDictionary*)dic{
    self.userAccountTextField.text = [dic objectForKey:@"username"];
}

-(void)initGameView{
    // 创建一个富文本对象
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    // 设置富文本对象的颜色
    attributes[NSForegroundColorAttributeName] = [UIColor grayColor];
    _userAccountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:attributes];
    _NameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入姓名" attributes:attributes];
    _idTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入身份证号" attributes:attributes];
    
    //输入框处理
    [_userAccountTextField addTarget:self action:@selector(LimitTextFieldLength2:) forControlEvents:UIControlEventEditingChanged];
    [_NameTextField addTarget:self action:@selector(LimitTextFieldLength:) forControlEvents:UIControlEventEditingChanged];
    [_idTextField addTarget:self action:@selector(LimitTextFieldLength1:) forControlEvents:UIControlEventEditingChanged];
    _idTextField.delegate = self;
    _NameTextField.delegate = self;
//    _userAccountTextField.delegate = self;
}

#pragma mark - 提交
- (IBAction)commitBtnClick:(id)sender {
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.NameTextField resignFirstResponder];
    [self.idTextField resignFirstResponder];
    NSDictionary* dic = [[NSDictionary alloc] initWithObjects:@[self.userAccountTextField.text, self.NameTextField.text ,self.idTextField.text] forKeys:@[@"username", @"name",@"cardId"]];
    if (kUseJs) {
        [[HWGameMgr shared] callToJsWithMethed:kSDKShowRealNameCallBack andDic:dic];
    } else {
        if (self.NameTextField.text == NULL) {
            [[HWGameMgr shared] showToastWithMsg:@"请输入姓名"];
            return;
        }
        if (self.idTextField.text == NULL) {
            [[HWGameMgr shared] showToastWithMsg:@"请输入身份证号"];
            return;
        }
        [[HWGameMgr shared] httpRealName:@{@"truename":self.NameTextField.text,@"idcard":self.idTextField.text} ];
    }
}


- (void)switchToLogin{
//    _title.text = @"登录";
//    _tips.text = @"没有账号？点此";
//    [_switchBtn setTitle:@"注册" forState:UIControlStateNormal];
//    [_switchBtn setTitle:@"注册" forState:UIControlStateSelected];
//    [_regLoginBtn setTitle:@"登录" forState:UIControlStateNormal];
//    [_regLoginBtn setTitle:@"登录" forState:UIControlStateSelected];
}

#pragma mark - 限制文字长度
-(void) LimitTextFieldLength:(id)sender{
//    if (_NameTextField.text.length>18) {
//        _NameTextField.text = [_NameTextField.text substringToIndex:18];
//    }
}
#pragma mark - 限制文字长度
-(void) LimitTextFieldLength1:(id)sender{
    if (_idTextField.text.length>18) {
        _idTextField.text = [_idTextField.text substringToIndex:18];
    }
}
#pragma mark - 限制文字长度
-(void) LimitTextFieldLength2:(id)sender{
    if (_userAccountTextField.text.length>kAccountLength) {
        _userAccountTextField.text = [_userAccountTextField.text substringToIndex:kAccountLength];
    }
}


#pragma mark - 点击事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.NameTextField resignFirstResponder];
    [self.idTextField resignFirstResponder];
}

#pragma mark - delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField!=_idTextField) {
        return YES;
    }
    NSMutableString *str = [NSMutableString stringWithFormat:@"%@",textField.text];

    [str replaceCharactersInRange:range withString:string];

    NSString *regex = @"[A-Za-z0-9]{0,18}";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    BOOL isValid = [predicate evaluateWithObject:str];

    return isValid;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}

@end
